FileUploader = require "./../../common/file-uploader/index.coffee"

$ () ->

	jisuan = (weight) ->
		if $("#height").val() != ""
			_tempHeight = $("#height").val()/100
			_temp = _tempHeight*_tempHeight
			temp = (weight / _temp).toFixed(1)
			return temp

	$("#f_weight").blur (e) ->
		$("#f_bmi").val(jisuan($(e.target).val()))

	$("#r_weight").blur (e) ->
		$("#r_bmi").val(jisuan($(e.target).val()))


	$("#province").change (e) ->
		param =
			'province_id':$("#province").val()

		$.get '/api/admin/address/get-city-province-id',param, (res) ->
			result = "<option value=''>-请选择-</option>"
			# console.log res[0]
			for city in res.data
				result = result + "<option value='"+city.id+"'>"+city.city_name+"</option>"
			$("#city").html(result)

	imgKeyValue1 = null

	new FileUploader {
        click_button: "upload1_btn", # 点击后触发打开上传文件窗口的按钮，与类型为file的按钮绑定
        show_img: "cn_img1",                   # 上传成功后回显的图片ID
        domain: "http://osr47xq4z.bkt.clouddn.com",    # bucket 域名，下载资源时用到，**必需**
        browse_button: 'upload1',       # 上传选择的点选按钮ID，**必需**
        container: 'img-wrapper1',       # 上传区域DOM ID，默认是browser_button的父元素，**必需**
    }, {
        FileUploaded: (up, file, info)->
            imgKeyValue1 = info.key
	}

	imgKeyValue2 = null

	new FileUploader {
        click_button: "upload2_btn", # 点击后触发打开上传文件窗口的按钮，与类型为file的按钮绑定
        show_img: "cn_img2",                   # 上传成功后回显的图片ID
        domain: "http://osr47xq4z.bkt.clouddn.com",    # bucket 域名，下载资源时用到，**必需**
        browse_button: 'upload2',       # 上传选择的点选按钮ID，**必需**
        container: 'img-wrapper2',       # 上传区域DOM ID，默认是browser_button的父元素，**必需**
    }, {
        FileUploaded: (up, file, info)->
            imgKeyValue2 = info.key
	}

	imgKeyValue3 = null

	new FileUploader {
        click_button: "upload3_btn", # 点击后触发打开上传文件窗口的按钮，与类型为file的按钮绑定
        show_img: "cn_img3",                   # 上传成功后回显的图片ID
        domain: "http://osr47xq4z.bkt.clouddn.com",    # bucket 域名，下载资源时用到，**必需**
        browse_button: 'upload3',       # 上传选择的点选按钮ID，**必需**
        container: 'img-wrapper3',       # 上传区域DOM ID，默认是browser_button的父元素，**必需**
    }, {
        FileUploaded: (up, file, info)->
            imgKeyValue3 = info.key
	}

	$(".create_btn").click (e) ->
		param = 
			case_type: $(".case_type").val()
			title: $("#title").val()
			desc: $("#desc").val()
			history: $("#history").val()
			name: $("#name").val()
			age: $("#age").val()
			sex: $('input:radio[name="sex"]').val()
			province_id: $("#province").val()
			city_id: $("#city").val()
			manner: $("#manner").val()
			time: $("#time").val()
			height: $("#height").val()
			introduce: ue.getContent()
			picture: imgKeyValue1

			f_weight: $("#f_weight").val()
			f_bmi: $("#f_bmi").val()
			f_waistline: $("#f_waistline").val()
			f_hipline: $("#f_hipline").val()
			f_picture: imgKeyValue2
			f_level: $("#f_level").val()
			f_gi: $("#f_gi").val()

			r_weight: $("#r_weight").val()
			r_bmi: $("#r_bmi").val()
			r_waistline: $("#r_waistline").val()
			r_hipline: $("#r_hipline").val()
			r_picture: imgKeyValue3
			r_level: $("#r_level").val()
			r_gi: $("#r_gi").val()

		$.post "/api/admin/case/create", param, (res) ->
    		if res.status.errCode is 0
               alert "创建成功"
               window.location.href = "/admin/case/list"
            else
               alert res.status.message